<?php

require( "functions.php" );

$topNode = LoggerCreateXMLDocument();

if ( LoggerOpenDB() )
{
	if ( isset( $_POST[ "net_id" ], $_POST[ "roster_id" ], $_POST[ "name" ], $_POST[ "location" ],
		$_POST[ "is_net_control" ], $_POST[ "is_alt_control" ], $_POST[ "update_id" ], $_POST[ "prev_update_id" ],
		$_POST[ "is_skywarn" ], $_POST[ "has_traffic" ], $_POST[ "is_mobile" ], $_POST[ "is_active" ],
		$_POST[ "time_in" ], $_POST[ "logger_id" ], $_POST[ "is_posted" ], $_POST[ "has_rev_geo" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$roster_id = mysql_real_escape_string( rawurldecode( $_POST[ "roster_id" ] ) );
		$name = mysql_real_escape_string( rawurldecode( $_POST[ "name" ] ) );
		$location = mysql_real_escape_string( utf8_decode( rawurldecode( $_POST[ "location" ] ) ) );
		if ( isset( $_POST[ "latitude" ], $_POST[ "longitude" ] ) )
		{
			$latitude = ( double ) $_POST[ "latitude" ];
			$longitude = ( double ) $_POST[ "longitude" ];
			$insertColumns = "latitude, longitude,";
			$insertValues = "$latitude, $longitude,";
			$rosterUpdate = "latitude = $latitude, longitude = $longitude, is_gps = 0,";
		}
		else
		{
			$insertColumns = $insertValues = $rosterUpdate = "";
		}
		$is_net_control = ( int ) $_POST[ "is_net_control" ];
		$is_alt_control = ( int ) $_POST[ "is_alt_control" ];
		$update_id = mysql_real_escape_string( rawurldecode( $_POST[ "update_id" ] ) );
		$prev_update_id = mysql_real_escape_string( rawurldecode( $_POST[ "prev_update_id" ] ) );
		$is_skywarn = ( int ) $_POST[ "is_skywarn" ];
		$has_traffic = ( int ) $_POST[ "has_traffic" ];
		$is_mobile = ( int ) $_POST[ "is_mobile" ];
		$is_active = ( int ) $_POST[ "is_active" ];
		$time_in = mysql_real_escape_string( rawurldecode( $_POST[ "time_in" ] ) );
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$is_posted = ( int ) $_POST[ "is_posted" ];
		$has_rev_geo = ( int ) $_POST[ "has_rev_geo" ];
		if ( isset( $_POST[ "aprs_time" ] ) )
		{
			$aprs_time = "'" . mysql_real_escape_string( rawurldecode( $_POST[ "aprs_time" ] ) ) . "'";
		}
		else
		{
			$aprs_time = "NULL";
		}
		if ( isset( $_POST[ "time_out" ] ) )
		{
			$time_out = "'" . mysql_real_escape_string( rawurldecode( $_POST[ "time_out" ] ) ) . "'";
		}
		else
		{
			$time_out = "NULL";
		}
		if ( isset( $_POST[ "aprs_id" ] ) )
		{
			$aprsIdValue = "'" . mysql_real_escape_string( rawurldecode( $_POST[ "aprs_id" ] ) ) . "'";
		}
		else
		{
			$aprsIdValue = "NULL";
		}
		if ( $is_posted && isset( $_POST[ "prev_roster_id" ] ) )
		{
			$prev_roster_id = mysql_real_escape_string( rawurldecode( $_POST[ "prev_roster_id" ] ) );
			$rosterUpdate .= " prev_roster_id = roster_id, roster_id = '$roster_id',";
			$formerUpdate = "roster_id = '$roster_id',";
			$rosterWhere = "roster_id = '$prev_roster_id'";
		}
		else
		{
			$rosterUpdate .= " prev_roster_id = NULL,";
			$formerUpdate = "";
			$rosterWhere = "roster_id = '$roster_id'";
		}
		if ( !$is_posted )
		{
			$sql = "INSERT INTO roster ( net_id, roster_id, name, location, $insertColumns is_net_control, is_alt_control, is_skywarn, has_traffic, " .
				"is_mobile, is_active, time_in, logger_id, time_out, aprs_id, aprs_time, has_rev_geo, update_id ) VALUES ( $net_id, '$roster_id', '$name', '$location', $insertValues $is_net_control, " .
				"$is_alt_control, $is_skywarn, $has_traffic, $is_mobile, $is_active, '$time_in', '$logger_id', $time_out, $aprsIdValue, $aprs_time, $has_rev_geo, '$update_id' )";
			if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
				$error = "Roster insert failure";
		}
		else
		{
			$sql = "UPDATE roster SET name = '$name', location = '$location', $rosterUpdate is_net_control = $is_net_control, " .
				"is_alt_control = $is_alt_control, is_skywarn = $is_skywarn, has_traffic = $has_traffic, is_mobile = $is_mobile, is_active = $is_active, " .
				"time_in = '$time_in', logger_id = '$logger_id', time_out = $time_out, aprs_id = $aprsIdValue, aprs_time = $aprs_time, has_rev_geo = $has_rev_geo, update_id = '$update_id' " .
				"WHERE net_id = $net_id AND $rosterWhere AND update_id = '$prev_update_id'";
			if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
				$error = "Roster update failure";
		}
		if ( !isset( $error ) && isset( $latitude ) && $latitude != 0 && !$is_mobile )
		{
			$sql = "UPDATE former_members SET name = '$name', $formerUpdate location = '$location', latitude = $latitude, longitude = $longitude, aprs_id = $aprsIdValue WHERE $rosterWhere";
			if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
			{
				$sql = "INSERT INTO former_members ( roster_id, name, location, latitude, longitude, aprs_id ) VALUES " .
					"( '$roster_id', '$name', '$location', $latitude, $longitude, $aprsIdValue )";
				mysql_query( $sql );
			}
		}
		if ( !isset( $error ) && isset( $prev_roster_id ) )
		{
			$sql = "UPDATE activity SET roster_id = '$roster_id' WHERE net_id = $net_id AND $rosterWhere";
			mysql_query( $sql );
		}
	}
	else
	{
		$error = "Submission failure";
	}
}
else
{
	$error = "DB open failure";
}

if ( isset( $error ) )
{
	LoggerAppendError( $topNode, $error );
}

$doc = $topNode->ownerDocument;
echo $doc->saveXML();

?>